Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class ColoredProgressBar
    Inherits Control

    Private m_Bar As New ProgressBar()

    Public Sub New()
        Controls.Add(m_Bar)
        m_Bar.Dock = DockStyle.Fill
        MyBase.Size = New Size(100, 32)
        SetProgressBackColor(MyBase.BackColor)
        SetProgressForeColor(MyBase.ForeColor)
    End Sub

    Protected Overrides Sub OnBackColorChanged(ByVal e As EventArgs)
        MyBase.OnBackColorChanged(e)
        SetProgressBackColor(MyBase.BackColor)
    End Sub

    Protected Overrides Sub OnForeColorChanged(ByVal e As EventArgs)
        MyBase.OnForeColorChanged(e)
        SetProgressBackColor(MyBase.ForeColor)
    End Sub

    Public Property Maximum() As Integer
        Get
            Return m_Bar.Maximum
        End Get
        Set(ByVal value As Integer)
            Try
                m_Bar.Maximum = value
            Catch e As Exception
                Throw e
            End Try
        End Set
    End Property

    Public Property Minimum() As Integer
        Get
            Return m_Bar.Minimum
        End Get
        Set(ByVal value As Integer)
            Try
                m_Bar.Minimum = value
            Catch e As Exception
                Throw e
            End Try
        End Set
    End Property

    Public Property Value() As Integer
        Get
            Return m_Bar.Value
        End Get
        Set(ByVal value As Integer)
            Try
                m_Bar.Value = value
            Catch e As Exception
                Throw e
            End Try
        End Set
    End Property

    <DllImport("User32.Dll")> _
    Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function

    Private Const PBM_SETBKCOLOR As Integer = &H2001
    Private Const PBM_SETBARCOLOR As Integer = &H409

    Private Sub SetProgressBackColor(ByVal c As Color)
        Dim tmpColor As Color = Color.FromArgb(c.R, c.G, c.B)
        Dim oleColor As Integer = ColorTranslator.ToOle(tmpColor)
        SendMessage(m_Bar.Handle, PBM_SETBKCOLOR, 0, oleColor)
    End Sub

    Private Sub SetProgressForeColor(ByVal c As Color)
        Dim tmpColor As Color = Color.FromArgb(c.R, c.G, c.B)
        Dim oleColor As Integer = ColorTranslator.ToOle(tmpColor)
        SendMessage(m_Bar.Handle, PBM_SETBARCOLOR, 0, oleColor)
    End Sub

End Class
